package com.litengsoft.lianmeng.common;

import com.litengsoft.lianmeng.common.enums.ResultCodeEnum;

import java.io.Serializable;

/**
 * Created by zoushaofei on 17-04-10.
 */
public class Result<V> implements Serializable {

    /**
     * 请求是否成功
     */
    private boolean success;

    /**
     * 响应码
     */
    private int code;

    /**
     * 响应信息
     */
    private String info;

    /**
     * 返回值
     */
    private V data;

    /**
     * 分页信息
     */
    private PageBean pageBean;

    public boolean isSuccess() {
        return success;
    }

    private void setSuccess(boolean success) {
        this.success = success;
    }

    public V getData() {
        return data;
    }

    public void setData(V data) {
        this.data = data;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    public PageBean getPageBean() {
        return pageBean;
    }

    public void setPageBean(PageBean pageBean) {
        this.pageBean = pageBean;
    }


    public Result() {
    }

    public Result<V> build(V data) {
        return build(ResultCodeEnum.SUCCESS, data, null);
    }

    public Result<V> build(V data, PageBean pageBean) {
        return build(ResultCodeEnum.SUCCESS, data, pageBean);
    }

    public Result<V> build(ResultCodeEnum code) {
        return build(code, null, null);
    }

    public Result<V> build(ResultCodeEnum code, String message) {
        return build(code, message, null, null);
    }

    public Result<V> build(ResultCodeEnum codeEnum, V data, PageBean pageBean) {
        this.success = codeEnum.getCode() == ResultCodeEnum.SUCCESS.getCode();
        this.code = codeEnum.getCode();
        this.info = codeEnum.getInfo();
        this.data = data;
        this.pageBean = pageBean;
        return this;
    }

    public Result<V> build(ResultCodeEnum codeEnum, String message, V data, PageBean pageBean) {
        this.success = codeEnum.getCode() == ResultCodeEnum.SUCCESS.getCode();
        this.code = codeEnum.getCode();
        this.info = message;
        this.data = data;
        this.pageBean = pageBean;
        return this;
    }

    public Result<V> build(ResultCodeEnum codeEnum, V data) {
        this.success = codeEnum.getCode() == ResultCodeEnum.SUCCESS.getCode();
        this.code = codeEnum.getCode();
        this.info = codeEnum.getInfo();
        this.data = data;
        return this;
    }

    public Result<V> error(ResultCodeEnum codeEnum) {
        return error(codeEnum, codeEnum.getInfo());
    }

    public Result<V> error(ResultCodeEnum codeEnum, String message) {
        this.success = false;
        this.code = codeEnum.getCode();
        this.info = message;
        return this;
    }

}